//
//  ViewController.m
//  TurnOffLights
//
//  Created by aaaaaa on 16/6/7.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, retain) NSMutableArray<NSArray *> *array;
@property (nonatomic, retain) NSMutableArray<UIButton *> *singleArray;

@end

@implementation ViewController

- (void)dealloc
{
    [_array release];
    [_singleArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTurnOffLights];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createTurnOffLights {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(4.5, 100, 405, 405)];
    [self.view addSubview:view];
    [view release];
    
    self.array = [NSMutableArray arrayWithCapacity:0];
    
    for (int i = 0; i < 5; i++) {
        self.singleArray = [NSMutableArray arrayWithCapacity:0];
        for (int j = 0; j < 5; j++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(81 * i, 81 * j, 81, 81);
            button.layer.borderWidth = 1.0;
            button.layer.borderColor = [UIColor blackColor].CGColor;
            
            
            NSInteger a = arc4random() % 2;
            if (a == 0) {
                button.backgroundColor = [UIColor whiteColor];
                button.tag = 0;
            } else{
                button.backgroundColor = [UIColor orangeColor];
                button.tag = 1;
            }
            
            [button addTarget:self action:@selector(changeAction:) forControlEvents:UIControlEventTouchUpInside];
            
            [view addSubview:button];
            [self.singleArray addObject:button];
        }
        [self.array addObject:self.singleArray];
    }
    
    
}



- (void)changeAction:(UIButton *)button{
    
    for (int i = 0; i < 5; i++) {
        NSInteger a = 0;
        NSArray *array = self.array[i];
        for (int j = 0; j < 5; j++) {
            if (button == array[j]) {
                a = 1;
                [self changeButton:button];
                
                if (i - 1 >= 0) {
                    UIButton *aButton = self.array[i - 1][j];
                    [self changeButton:aButton];
                }
                
                if (i + 1 < 5) {
                    UIButton *aButton = self.array[i + 1][j];
                    [self changeButton:aButton];
                }
                
                if (j - 1 >= 0) {
                    UIButton *aButton = self.array[i][j - 1];
                    [self changeButton:aButton];
                }
                
                if (j + 1 < 5) {
                    UIButton *aButton = self.array[i][j + 1];
                    [self changeButton:aButton];
                }
                break;
                
            }
        }
        
        if (a == 1) {
            break;
        }
    }
    
}

- (void)changeButton:(UIButton *)button
{
    if (button.tag == 0) {
        button.tag = 1;
        button.backgroundColor = [UIColor orangeColor];
    } else {
        button.tag = 0;
        button.backgroundColor = [UIColor whiteColor];
    }
}



@end
